<template>
    <Teleport to="body">
        <transition name="el-fade-in">
            <div class="homepage-list-dialog" v-if="visible">
                <div class="homepage-list-dialog__content">
                    <img class="icon-close" :src="_oss_ + 'images/icon-close-default.png'" @click="onClose" />

                    <div class="tabs">
                        <div class="tab-item" @click="onActiveTab(item.id)" :class="{ active: item.id == activeTab }"
                            v-for="item in tabs" :key="item.id">
                            {{ item.name }}({{ item.value }})
                        </div>
                    </div>

                    <div class="like-list" v-if="activeTab == 1">
                        <div class="like-item" v-for="item in list" :key="item.id">
                            <div class="user-group" @click="GoToPageHome(item.user.id)">
                                <img class="avatar" :src="item.user.avatar || _defaultAvatar_" />
                                <div class="user-name">{{ item.user.nickname }}</div>
                            </div>

                            <div class="tag">点赞了你的作品</div>

                            <div class="work-image" @click="onJumpWork(item.work.id, item.work.templatestype_id)">
                                <img :src="item.work.file" />
                            </div>
                        </div>
                    </div>

                    <div class="friend-list" v-if="activeTab == 2">
                        <div class="item" v-for="item in list" :key="item.id">
                            <div class="user-group" @click="GoToPageHome(item.user.id)">
                                <img class="avatar" :src="item.user.avatar || _defaultAvatar_" />
                                <div class="user-name">{{ item.user.nickname }}</div>
                            </div>

                            <div class="btn-group">
                                <div class="btn-primary" @click="onDialogue(item.user.id)">私信</div>
                                <div :class="item.is_follow ? 'btn-default' : 'btn-primary'"
                                    @click="item.is_follow ? onUnFollow(item) : onFollow(item)">
                                    {{ item.is_follow ? '已关注' : '关注' }}
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="follow-list" v-if="activeTab == 3">
                        <div class="item" v-for="item in list" :key="item.id">
                            <div class="user-group" @click="GoToPageHome(item.user.id)">
                                <img class="avatar" :src="item.user.avatar || _defaultAvatar_" />
                                <div class="user-name">{{ item.user.nickname }}</div>
                            </div>

                            <div class="btn-group">
                                <div :class="item.is_follow ? 'btn-default' : 'btn-primary'"
                                    @click="item.is_follow ? onUnFollow(item) : onFollow(item)">
                                    {{ item.is_follow ? '已关注' : '关注' }}
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="fans-list" v-if="activeTab == 4">
                        <div class="item" v-for="item in list" :key="item.id">
                            <div class="user-group" @click="GoToPageHome(item.user.id)">
                                <img class="avatar" :src="item.user.avatar || _defaultAvatar_" />
                                <div class="user-name">{{ item.user.nickname }}</div>
                            </div>

                            <div class="btn-group">
                                <div :class="item.is_follow ? 'btn-default' : 'btn-primary'"
                                    @click="item.is_follow ? onUnFollow(item) : onFollow(item)">
                                    {{ item.is_follow ? '已关注' : '关注' }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </transition>
    </Teleport>

    <DialogDialogueP ref="dialogueDialogRef" />
</template>
<script setup>
import { GetHomepageDetail, GetMyHomepage, Follow, UnFollow, SetTaskScore } from "@/api";
const visible = ref(false);
const list = ref([]);
const userStore = useUserStore();
const activeTab = ref(1);
const dialogueDialogRef = ref(null);
const tabs = reactive([
    {
        id: 1,
        name: "获赞",
        value: 0
    },
    {
        id: 2,
        name: "朋友",
        value: 0
    },
    {
        id: 3,
        name: "关注",
        value: 0
    },
    {
        id: 4,
        name: "粉丝",
        value: 0
    }
]);

const getHomepageDetail = async (type) => {
    const res = await GetHomepageDetail({ uid: userStore.userInfo.id, type });

    if (res.code == 200) {
        list.value = res.data;
    }
}

const onJumpWork = (id, templatestype_id) => {
    let origin = GetWorkLink3(templatestype_id);

    if (userStore.isLogin) {
        SetTaskScore({
            code: 1003,
            _id: id,
        });

        window.open(`https://${origin}/?${aesEncrypt(`tid=${id}&type=works&uid=${userStore.userInfo.id}`)}`);
    } else {
        LoginModal();
    }
}

const onClose = () => {
    list.value = [];
    visible.value = false;
}

const onDialogue = (uid) => {
    dialogueDialogRef.value.open(uid);
}

const onFollow = async (item) => {
    if (userStore.isLogin) {
        const res = await Follow({ follow_id: item.user.id });
        if (res.code === 200) {
            getMyHomepage();
            item.is_follow = true;
            Message().success("关注成功");
        } else {
            Message().warning(res.msg);
        }
    } else {
        LoginModal();
    }
}

const onUnFollow = async (item) => {
    if (userStore.isLogin) {
        const res = await UnFollow({ follow_id: item.user.id });
        if (res.code === 200) {
            getMyHomepage();
            item.is_follow = false;
            Message().success("取消关注成功");
        } else {
            Message().warning(res.msg);
        }
    } else {
        LoginModal();
    }
}

const getMyHomepage = async () => {
    const res = await GetMyHomepage();
    tabs[0].value = res.data.like_num;
    tabs[1].value = res.data.friend_num;
    tabs[2].value = res.data.follow_num;
    tabs[3].value = res.data.fans_num;
}

const onActiveTab = (type) => {
    activeTab.value = type;
    list.value = [];
    getHomepageDetail(activeTab.value);
}

defineExpose({
    async open(type) {
        activeTab.value = type;
        await getHomepageDetail(type);
        await getMyHomepage();
        visible.value = true;
    }
})
</script>
<style lang="scss" scoped>
.homepage-list-dialog {
    position: fixed;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999;

    &__content {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 500px;
        height: 580px;

        background: #FFFFFF;
        border-radius: 20px;
        transform: translate(-50%, -50%);

        .icon-close {
            position: absolute;
            right: 25px;
            top: 27px;
            width: 16px;
            height: 16px;
            cursor: pointer;
        }

        .tabs {
            display: flex;
            align-items: center;
            height: 70px;
            border-bottom: 1px solid #E9E9E9;
            padding: 0 25px;

            .tab-item {
                margin-right: 30px;
                font-size: 18px;
                cursor: pointer;
                color: #999;

                &.active {
                    color: #5085FB;
                }
            }
        }

        .user-group {
            display: flex;
            align-items: center;
            cursor: pointer;

            .avatar {
                width: 42px;
                height: 42px;
                border-radius: 50%;
                margin-right: 16px;
                object-fit: cover;
            }
        }

        .like-list {
            height: calc(100% - 70px);
            overflow: auto;
            padding: 13px 25px 0;

            .like-item {
                display: flex;
                align-items: center;
                margin-bottom: 13px;

                .tag {
                    font-size: 14px;
                    color: #666;
                    margin-left: 16px;
                }

                .work-image {
                    width: 91px;
                    height: 52px;
                    margin-left: auto;
                    border-radius: 4px;
                    overflow: hidden;
                    cursor: pointer;

                    img {
                        width: 100%;
                        height: 100%;
                        object-fit: cover;
                    }
                }
            }
        }

        .friend-list,
        .follow-list,
        .fans-list {
            height: calc(100% - 70px);
            padding: 0 25px;
            overflow: auto;

            .item {
                height: 78px;
                display: flex;
                align-items: center;
                border-bottom: 1px solid #F1F1F1;
            }
        }


        .btn-group {
            display: flex;
            margin-left: auto;
            gap: 0 11px;

            .btn-primary,
            .btn-default {
                width: 60px;
                height: 30px;

                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 14px;
                border-radius: 4px;
                cursor: pointer;
            }

            .btn-primary {
                background: #5085FB;
                color: #fff;
            }

            .btn-default {
                color: #333333;
                background: #EBEDEF;
            }
        }
    }
}
</style>